package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException {
        System.out.println("Servlet initialized.");
        super.init();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html");

        // 显式关闭 PrintWriter 流
        try (java.io.PrintWriter out = response.getWriter()) {
            // 输出 HTML 内容
            out.println("<html><body>");
            out.println("<h1>Hello, Servlet!</h1>");
            out.println("</body></html>");
        }
    }

    @Override
    public void destroy() {
        System.out.println("Servlet destroyed.");
        super.destroy();
    }
}
